<?php
header('Content-Type: application/json');
	require_once("util.php");
	require_once("sql.php");
	require_once("statutory.php");
?>

<?php 
if($_SERVER['REQUEST_METHOD'] == "GET"){
	
	$statutories=array();
	$responseJson="";
	
	//Get the fulldetils of company(for display in table)
	if (array_key_exists("fulldetails",$_REQUEST)){
	
			$responseJson .="[";
		
			$util=new DBUtil();
			$conn=$util->getConnection();
			$sql = SQLQuery::STATUTORY_ALL_SQL;
			$stmt = $conn->prepare($sql);
			$stmt->execute();
			
			$result = $stmt->get_result();
						
			if ($result->num_rows > 0) {
							while($row = $result->fetch_array()) {
												
							$responseJson .="{";
							$responseJson .= '"id":"' .$row["ID"] .'",';
							$responseJson .= '"name":"' .$row["NAME"] .'",';
							$responseJson .= '"description":"' .$row["DESCRIPTION"] .'"';
							$responseJson .="},";
							}
					$responseJson=substr($responseJson,0,strlen($responseJson)-1);
			}
			$conn->close();
		
			$responseJson .="]"; 
	 
	}else {//for update & delete operation, get the information about the selected company
		if (array_key_exists("key",$_REQUEST)){
		
			$statutoryPK= $_REQUEST["key"];
			
			$util=new DBUtil();
			$conn=$util->getConnection();
			$sql = SQLQuery::STATUTORY_FIND_BYID;
			$stmt = $conn->prepare( $sql);
			$stmt->bind_param('d',$statutoryPK);
			$stmt->execute();
			
			$result = $stmt->get_result();
			
			if ($result->num_rows > 0) {
				while($row = $result->fetch_array()) {
				
				$statutory_name =$row["NAME"];
				$statutory_description=$row["DESCRIPTION"];
				
				$responseJson='[{"id":"statutory_name","label":"Statutory Name","display":"true","value":"' .$statutory_name .'","type":"input_text"} ,{"id":"statutory_desc","label":"Statutory Description","display":"true","value":"' .$statutory_description .'","type":"input_text"}]';
				}
			}
		$conn->close();	
			

		}else {//For create operation
			$responseJson='[{"id":"statutory_name","label":"Statutory Name","display":"true","type":"input_text"}, {"id":"statutory_desc","label":"Statutory Description","display":"true","type":"input_text"}]';
		}
	}
	echo $responseJson;
	}
elseif ($_SERVER['REQUEST_METHOD'] == "POST"){
	if (array_key_exists("action",$_REQUEST)){
		$action= $_REQUEST["action"];
		
		$success ='{"status" :"success"}';
		$failure ='{"status" :"faiure"}';
		
		if($action=="create"){
			$statutoryManager=new ManageStatutory();
			$status=$statutoryManager->addStatutory($_REQUEST["statutory_name"],$_REQUEST["statutory_desc"]);
			if($status){echo $success;}
			else {echo $failure;}
		}elseif($action=="update"){
			$statutoryManager=new ManageStatutory();
			$status=$statutoryManager->updateStatutory($_REQUEST["key"],$_REQUEST["statutory_name"],$_REQUEST["statutory_desc"]);
			if($status){echo $success;}
			else {echo $failure;}
		}else if($action=="delete"){
			$statutoryManager=new ManageStatutory();
			$status=$statutoryManager->deleteStatutory($_REQUEST["key"]);
			if($status){echo $success;}
			else {echo $failure;}
		}else{
			echo $failure;
		}
		
	}else{
		echo $failure;
	}
}
/*---------------------------------------*/

class ManageStatutory {
	
	
	public function __construct(){
		}
	public  function addStatutory($statutory_name,$statutory_desc){
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::STATUTORY_ADD;
		$stmt = $conn->prepare($sql);
		$stmt->bind_param('ss',$statutory_name,$statutory_desc);
		$status=$stmt->execute();
		
		$conn->commit();
		$conn->close();
		return $status;
	}
	
	public  function updateStatutory($id,$name,$description){
		
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::STATUTORY_UPDATE;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('ssd',$name,$description,$id);
		$status=$stmt->execute();
		
		$conn->commit();
		$conn->close();
		return $status;
	}
	public function deleteStatutory($id){
		
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::STATUTORY_DELETE;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('d',$id);
		$status=$stmt->execute();
		
		$conn->commit();
		$conn->close();
		return $status;
	}
	
}

?>
